package cn.gz.demo.utils;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtils {
    private static ThreadPoolExecutor threadPoolExecutor;

    public static ThreadPoolExecutor createThreadPool() {
        if (threadPoolExecutor != null) {
            return threadPoolExecutor;
        }
        /**
         *   corePoolSize：线程池中核心线程数的最大值
         *   maximumPoolSize：线程池中能拥有最多线程数
         *   keepAliveTime：表示空闲线程的存活时间。
         *   TimeUnitunit：表示keepAliveTime的单位
         *   workQueue：用于缓存任务的阻塞队列
         */
        int corePoolSize = Runtime.getRuntime().availableProcessors();
        int maxNumPoolSize = corePoolSize + 1;
        long keepAliveTime = 1;
        TimeUnit unit = TimeUnit.HOURS;
        threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
                maxNumPoolSize,
                keepAliveTime,
                unit,
                new LinkedBlockingQueue<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        return threadPoolExecutor;
    }
}
